const app = getApp()
Page({

  data: {
    ////////fabiao
    icontype: ["info_circle", "info"],

    ///////////分栏
    item: [], //渲染的列表
    current: 1,  //请求的页码
    currtab: 0,
    swipertab: [{
      name: '我的发布', orderStatus: 0
    }, { name: '我的收藏', orderStatus: 1 }]
    
  },

  /////////////////////分栏
  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo()
    this.messageShow()
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
    this.messageShow()

  },
  messageShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        that.talk()
        break
      case 1:
        that.shouCang()
        break

    }
  },

  ////////////////////分栏结束
///////////////////操作收藏
  
  onCollectionTap: function (e) {
    var that = this;
    var talkId = e.currentTarget.dataset.talkid;

    wx.request({
      url: app.globalData.baseUrl + '/shoucang/getShouCang',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        "talkId": talkId,
        "buyerOpenid": wx.getStorageSync("openid")
      },//传入的参数
      success: function (res) {

        var shouCang = res.data.shouCang;
        // console.log("shouCang", shouCang);
        if (shouCang == false) {
          wx.request({
            url: app.globalData.baseUrl + '/shoucang/insertShouCang',
            data: {//获取数据传到后台
              "buyerOpenid": wx.getStorageSync("openid"),
              "talkId": talkId,

            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              var result = res.data.success
              var toastText = "收藏成功！";
              if (result != true) {
                toastText = "收藏失败！";
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });

              that.onShow()

            }

          })
        } else {
          wx.request({
            url: app.globalData.baseUrl + '/shoucang/deleteShouCang',
            data: {//获取数据传到后台
              "buyerOpenid": wx.getStorageSync("openid"),
              "talkId": talkId,

            },
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              var result = res.data.success
              var toastText = "取消收藏成功！";
              if (result != true) {
                toastText = "取消收藏失败！";
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });

              that.onShow()

            }

          })


        }

      }
    })


  },

////////////////结束操作收藏


  ///////////////收藏
shouCang:function(){
  var that = this;
  let openid = wx.getStorageSync("openid");
  wx.request({
    url: app.globalData.baseUrl + '/shoucang/getShouCangList',
    method: 'GET',
    data: {
      "buyerOpenid": openid
    },//传入的参数
    success: function (res) {
      var list = res.data.shouCangList;
      if (list.length == 0) {
        var toastText = '没有收藏信息';
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 2000
        });
        that.setData({
          shouCangList: list
        });
      } else {
        ///////////////////
        var index = 0;
        list.forEach((r) => {  //array是后台返回的数据
          index = index + 1;
          r.collected = true;
          console.log(r.talkId, r.collected);
          console.log(r.talkId, r);

        ///////////////
          if (index == list.length) {
        that.setData({
          shouCangList: list
        });
        console.log('shouCangList:' + list);
          }
        });
      }
    }
  })
},



  ////////////////收藏结束

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    //that.talk();
    that.messageShow()
  },

 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //that.talk();
    that.messageShow()
  },

  ////////////////////删除说说
  deletTalk:function(e){
    var that = this;
    var talkId = e.currentTarget.dataset.talkid;
    console.log('talkId:' + talkId);
    wx.request({
      url: app.globalData.baseUrl + '/talk/deleteTalk',
      method: 'GET',
      data: {
        "talkId": talkId
      },//传入的参数
      success: function (res) {
        var result = res.data.success;
        if (result == false) {
          var toastText = '删除数据失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          var toastText = '删除成功';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
          that.onShow();
          //console.log('talkList:' + list);
        }
      }
    })
  },

  ///////////////

  //////////////查看详情
  talkDetail: function (e) {
    var talkId = e.currentTarget.dataset.talkid;
    wx.navigateTo({
      url: '../talkDetail/talkDetail?talkid=' + talkId
    })
  },



  //////////////////发表说说
  addTalk: function () {
    wx.navigateTo({
      url: '../z/z'
    })
  },

  /////////////////////

  /////////////说说列表
  talk: function () {
    var that = this;
    let openid = wx.getStorageSync("openid");
    wx.request({
      url: app.globalData.baseUrl + '/talk/talkList',
      method: 'GET',
      data: {
        "buyerOpenid": openid
      },//传入的参数
      success: function (res) {
        var list = res.data.talkList;
        if (list == null) {
          var toastText = '获取数据失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            talkList: list
          });
          console.log('talkList:' + list);
        }
      }
    })

  },


 

  /*******下拉刷新********* */
  // 页面下拉刷新

  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      talkList: null
    });
    setTimeout(function () {
      that.onShow()
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 1500);

  }
})